<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;

    protected $fillable = ["slug", "title", "excerpt", "body", "category_id"];

    protected $with = ["category", "author"];

    public function scopeFilter($query, array $filters)
    {
        if ($filters["search"] ?? false) {
            $query
                ->where("title", "like", "%" . $filters["search"] . "%")
                ->orWhere("excerpt", "like", "%" . $filters["search"] . "%");
        }
    }

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function author()
    {
        return $this->belongsTo(User::class, "user_id");
    }
}
